package mud;

import java.util.ArrayList;
import java.util.List;

import mud.events.EntityEvent;

/** Base class for things that can be observed, seen, and identified */
public abstract class Entity implements Interactable {

	/** the objects currently listening for notifications from this entity */
	private final List<Observer> observers;

    /**
	 * Constructor
	 */
	public Entity() {
	    observers = new ArrayList<>();
	}

    /**
     * @return a description of visible characteristics
     */
    @Override
    public abstract String getAppearance();

    /**
     * @return the name of the entity
     */
    @Override
    public abstract String getName();

    /**
     * @return the species, race, or general classification of the entity
     */
    @Override
    public abstract String getType();

	/**
	 * Adds an observer to the list of unit-event listeners
	 * @param observer the observer to add
	 */
	public void addObserver(Observer observer) {
        assert !observers.contains(observer);
		observers.add(observer);
	}

    /**
     * Removes an observer from the list of unit-event listeners
     * @param observer the observer to remove
     */
	public void removeObserver(Observer observer) {
	    assert observers.contains(observer);
        assert !this.equals(observer);
		observers.remove(observer);
	}

    /** 
	 * Notifies observers that something has just occurred 
	 * @param event the details of the occurrence
	 */
	protected void fireEvent(EntityEvent event) {
		for (Observer observer : observers) {
			observer.observe(event);
		}
	}
}
